package com.x.bff.app.controller;

import com.x.bff.app.factory.cms.ContentFactory;
import com.x.bff.app.model.vo.cms.ContentCreateReqVO;
import com.x.bff.app.model.vo.cms.ContentVO;
import com.x.bff.app.model.vo.cms.ItemToggleRelationSaveReqVO;
import com.x.bff.app.service.cms.ContentService;
import com.x.core.utils.BeanUtil;
import com.x.core.web.api.R;
import com.x.core.web.controller.BaseFrontendController;
import com.x.core.web.page.PageList;
import com.x.provider.api.general.enums.CmsOptions;
import com.x.provider.api.general.model.dto.cms.*;
import com.x.provider.api.general.service.CmsRpcService;
import com.x.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(tags = "内容管理服务，提供内容的统一管理，包括形象展示、动态、短视频等")
@Slf4j
@RestController
@RequestMapping("/app/content")
public class ContentController extends BaseFrontendController {

    private final ContentService contentService;
    private final CmsRpcService cmsRpcService;
    private final ContentFactory contentFactory;

    public ContentController(ContentService contentService,
                             CmsRpcService cmsRpcService,
                             ContentFactory contentFactory){
        this.contentService = contentService;
        this.cmsRpcService = cmsRpcService;
        this.contentFactory = contentFactory;
    }

    @ApiOperation(value = "创建内容")
    @PostMapping("/create")
    public R<Void> create(@RequestBody @Validated ContentCreateReqVO reqVO){
        reqVO.setCustomerId(getCurrentCustomerId());
        contentService.create(reqVO);
        return R.ok();
    }

    @ApiOperation(value = "查询用户的内容")
    @GetMapping("/list/customer/content")
    public R<PageList<ContentVO>> list(@RequestParam(required = false, defaultValue = "0") long cursor,
                                       @RequestParam int pageSize,
                                       @RequestParam @Min(1) Long customerId,
                                       @ApiParam("内容类型 5 形象展示") @RequestParam @NotEmpty Integer itemType){
        PageList<ContentDTO> contents = cmsRpcService.listMyContent(ContentMyReqDTO.builder().options(List.of(CmsOptions.CONTENT.name())).pageDomain(getPageDomain())
                .sessionCustomerId(getNullableCurrentCustomerId())
                .customerId(customerId).itemType(itemType).build()).getData();
        if (contents.isEmptyList()){
            return R.ok(new PageList<>());
        }
        return R.ok(PageList.map(contents, contentFactory.prepare(contents.getList())));
    }

    @ApiOperation(value = "查询内容详情")
    @GetMapping("/list/ids")
    public R<List<ContentVO>> listByIds(@ApiParam("内容类型 5 形象展示") @RequestParam @NotEmpty Integer itemType,
                                        @ApiParam("内容id,多个id用,分割") @RequestParam @NotEmpty String ids){
        List<ContentDTO> contentList = cmsRpcService.listContentByIds(ContentByIdsReqDTO.builder().idList(StringUtil.parse(ids))
                .sessionCustomerId(getNullableCurrentCustomerId())
                .options(List.of(CmsOptions.CONTENT.name(), CmsOptions.CONTENT_MEDIA.name())).build()).getData();
        if (CollectionUtils.isEmpty(contentList)){
            return R.ok(new ArrayList<>());
        }
        return R.ok(contentFactory.prepare(contentList));
    }

    @ApiOperation(value = "删除内容")
    @PostMapping("/delete")
    public R<Void> delete(@ApiParam("内容id") @RequestParam @NotEmpty Long id){
        cmsRpcService.deleteContent(ContentDeleteReqDTO.builder().customerId(getCurrentCustomerId()).idList(Arrays.asList(id)).build());
        return R.ok();
    }

    @ApiOperation(value = "切换目标对象之间的关系，目前支持用户和用户之间的喜欢关系 ")
    @PostMapping("/item/toggle/relation")
    public R<Void> star(@RequestBody @Validated ItemToggleRelationSaveReqVO reqVO){
        reqVO.setSessionCustomerId(getCurrentCustomerIdAndNotCheckLogin());
        contentService.toggleRelation(reqVO);
        return R.ok();
    }
}
